<?php

/**
 * Impress - Open source digital signage
 *
 * @link http://digitalsignage.tk
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @category Impress
 * @package Impress
 */

/**
 * This class provides access to data layer.
 *
 * @package Impress
 * @subpackage Models
 */

class ADS_LayoutFrameModel extends ADS_DefaultModel {

    public function countFrames() {
        return dibi::fetch('
          SELECT
            COUNT(*) as cnt
          FROM
            [:wp:ads_schema] AS t1
          INNER JOIN
            [:wp:posts] AS t2 ON t1.layoutId = t2.ID
          %if', (bool)$this->useFilter, 'WHERE %and', $this->filter
        )->cnt;
    }
    
    public function getFrames() {
        return dibi::fetchAll('
          SELECT            
            t1.frameId,
            t1.name,
            t1.position,
            t1.validFrom,
            t1.validTo,
            t2.post_title,
            t2.guid
          FROM
            [:wp:ads_schema] AS t1
          INNER JOIN
            [:wp:posts] AS t2 ON t1.layoutId = t2.ID
           %if', (bool)$this->useFilter, 'WHERE %and', $this->filter,
          '%if', (bool)$this->useOrderBy, 'ORDER BY %by', $this->orderBy,
          '%if', (bool)$this->useLimit, '%lmt %ofs', $this->limit, $this->offset
        );
    }
     
    public function getFrame($frameId) {
        if(!is_int($frameId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        return dibi::fetch('
          SELECT
            t1.frameId,
            t1.name,
            t1.layoutId,
            t1.position,
            t1.validFrom,
            t1.validTo
          FROM
            [:wp:ads_schema] AS t1
          WHERE
            frameId = %i
        ', $frameId);
    }
    
    public function getLayouts() {
        return dibi::fetchAll("
          SELECT
            t1.post_title,
            t1.ID
          FROM
            [:wp:posts] AS t1
          WHERE
            post_type = 'page'
          AND
            post_status = 'publish'
        ");
    }
    
    public function addFrame($frame){
        if(!is_array($frame)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %.',__METHOD__));
        }
        $frame['position'] = ((int)dibi::fetch('SELECT MAX(position) as position FROM [:wp:ads_schema]')->position)+1;
        dibi::query('INSERT INTO [:wp:ads_schema]', $frame);
    }
    
    public function editFrame($frameId, $frame){
        if(!is_array($frame) || !is_int($frameId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %.',__METHOD__));
        }

        dibi::query('UPDATE [:wp:ads_schema] SET', $frame, 'WHERE frameId = %i', $frameId);
    }
    
    public function removeFrame($frameId){
        if(!is_int($frameId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %.',__METHOD__));
        }
        $frame = $this->getFrame($frameId);
        //update positions
        dibi::query('
            UPDATE 
                [:wp:ads_schema] 
            SET 
                position = position - 1 
            WHERE  
                position > %i
        ', $frame->position);
        dibi::query('DELETE FROM [:wp:ads_schema] WHERE frameId = %i LIMIT 1', $frameId);
    }
    
    public function increaseFramePosition($frameId){        
        if(!is_int($frameId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        //get frame
        $frame = dibi::fetch('SELECT * FROM [:wp:ads_schema] WHERE frameId = %i',$frameId);
        //update frame on next position
        dibi::query('
            UPDATE 
                [:wp:ads_schema] 
            SET 
                position = position - 1 
            WHERE 
                position = %i + 1
        ', $frame->position);
        //update frame
        dibi::query('
            UPDATE 
                [:wp:ads_schema] 
            SET 
                position = position + 1 
            WHERE 
                frameId = %i 
        ', $frameId);
    }
    
    public function decreaseFramePosition($frameId){        
        if(!is_int($frameId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        //get frame
        $frame = dibi::fetch('SELECT * FROM [:wp:ads_schema] WHERE frameId = %i',$frameId);
        //update frame on next position
        dibi::query('
            UPDATE 
                [:wp:ads_schema] 
            SET 
                position = position + 1 
            WHERE 
                position = %i - 1
        ', $frame->position);
        //update frame
        dibi::query('
            UPDATE 
                [:wp:ads_schema] 
            SET 
                position = position - 1 
            WHERE 
                frameId = %i 
        ', $frameId);
    }

}
